package isquirrel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import net.roarsoftware.lastfm.Album;
import net.roarsoftware.lastfm.Artist;
import net.roarsoftware.lastfm.Tag;

public class Lastfm {

	private static String apiKey = "91439b369e163ba775a650872313e7e5";
//	private static String secret = "1349b36b01e0e804a6c2909a6d0ec72a"; //might need to interact with user in the future probably not

	public Collection<Artist> getSimilarArtists(String artist, int limit) {
		Collection<Artist> similarArtists = Artist.getSimilar(artist, limit, apiKey);
		return similarArtists;
	}
	
	
	/**
	 * 
	 * 
	 * @param keywords <p> An array of Interest.Label
	 * @param limitPerKeyword <p> if you pass a 0 results will be infinitely many</p>
	 * @return
	 */
	public HashMap<String, Collection<Artist>> getArtistsByKeywords(ArrayList<String> keywords, int limitPerKeyword) {
		HashMap<String, Collection<Artist>> topArtists = new HashMap<String, Collection<Artist>>();
		for (String keyword : keywords) {
			Collection<Artist> artists = Tag.getTopArtists(keyword, apiKey);
			if (artists == null || artists.size() == 0) {
				topArtists.put(keyword, null);
				continue;
			}
			else {
				if (limitPerKeyword != 0) {
					Vector<Artist>  artists_v = new Vector<Artist>(artists); 
					//switching the following two lines will be returning the same results everytime a call to this function is made
					Collections.shuffle(artists_v);
					artists_v.setSize(limitPerKeyword);
					topArtists.put(keyword, artists_v);
				}
				else {
					topArtists.put(keyword, artists);
				}
			}
		}
		return topArtists;
	}

	
	public Collection<Artist> getArtistsByKeywords(ArrayList<String> keywords, int limitPerKeyword, int limit) {
		Collection<Artist> artists = new LinkedList<Artist>();
		for (String keyword : keywords) {
			Collection<Artist> keywordArtist = Tag.getTopArtists(keyword, apiKey);
			if (keywordArtist == null || keywordArtist.size() == 0) {
				continue;
			}
			else {
				if (limitPerKeyword != 0 ) {
					limitPerKeyword = limitPerKeyword > keywordArtist.size() ? keywordArtist.size() : limitPerKeyword; 
					artists.addAll(new Vector<Artist>(keywordArtist).subList(0, limitPerKeyword));
				} else {
					artists.addAll(keywordArtist);
				}
			}
		}
		if (limit != 0) {
			limit = limit > artists.size() ? artists.size() : limit;
			List<Artist> artistsList = new Vector<Artist>(artists);
			Collections.shuffle(artistsList);
			return artistsList.subList(0, limit);
		}
		else {
			return artists;
		}
	}
	
	
	public Collection<Album> getTopAlbumsByKeywords(ArrayList<String> keywords) {
		Collection<Album> topAlbums = new ArrayList<Album>();
		for (String keyword : keywords) {
			Collection<Album> alb = Tag.getTopAlbums(keyword, apiKey);
			if (alb == null || alb.size() == 0) {
				continue;
			}
			else {
				topAlbums.addAll(alb);
			}
		}
		return topAlbums;
	}
	
	
}
